package com.wandian.game.sound
{
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import flash.events.*;
	import flash.media.*;
	import flash.utils.*;

	public class SoundTransformEx extends EventDispatcher
	{
		private var fNewVolumeValueForSeamlessVolumeChanging_num:Number;
		private var fSeamlessVolumeChangingTween_tw:Tween;
		private var fCurrentSoundVolume_num:Number;
		private var fSeamlessVolumeChangingTimeoutId_uint:uint;
		private var fDefferedVolumeChangeControllers_uint_arr:Array;
		private var fVolumeUpdateIntervalInMs_num:Number;
		private var fSoundOn_bl:Boolean;
		private var fSeamlessVolumeChangingDelta_num:Number;
		private var fSoundTransform_st:SoundTransform;
		public static const EVENT_SEAMLESS_VOLUME_CHANGING_STARTED:String="onSeamlessVolumeChangingStarted";
		private static const SEAMLESS_VOLUME_CHANGING_STEPS:Number=10;
		public static const EVENT_VOLUME_UPDATED:String="onVolumeValueUpdated";
		public static const EVENT_SEAMLESS_VOLUME_CHANGING_COMPLETED:String="onSeamlessVolumeChangingCompleted";

		public function SoundTransformEx(param1:SoundTransform):void
		{
			this.fSoundTransform_st=param1;
			this.fCurrentSoundVolume_num=this.fSoundTransform_st.volume;
			this.fDefferedVolumeChangeControllers_uint_arr=new Array();
			return;
		} // end function

		private function onSeamlessVolumeChangingCompleted(event:Event=null):void
		{
			if (event)
			{
				Tween(event.target).removeEventListener(TweenEvent.MOTION_FINISH, this.onSeamlessVolumeChangingCompleted);
			}
			this.dispatchEvent(new Event(EVENT_SEAMLESS_VOLUME_CHANGING_COMPLETED));
			return;
		} // end function

		private function onVolumeValueUpdated():void
		{
			if (this.soundOn)
			{
				this.fSoundTransform_st.volume=this.fCurrentSoundVolume_num;
			}
			else
			{
				this.fSoundTransform_st.volume=0;
			}
			this.dispatchEvent(new Event(EVENT_VOLUME_UPDATED));
			return;
		} // end function

		public function setVolume(param1:Number, param2:Number=0, param3:Number=0, param4:Function=null):void
		{
			this.checkSetVolumeParams(param1, param2);
			if (param3 != 0)
			{
				this.fDefferedVolumeChangeControllers_uint_arr.push(setTimeout(this.setVolume, param3, param1, param2, 0, param4));
				return;
			}
			this.stopSeamlessVolumeChanging();
			if (param2 == 0)
			{
				this.volumeValue=param1;
			}
			else
			{
				this.startSeamlessVolumeChanging(param1, param2, param4);
			}
			return;
		} // end function

		private function startSeamlessVolumeChanging(param1:Number, param2:Number, param3:Function=null):void
		{
			this.fNewVolumeValueForSeamlessVolumeChanging_num=param1;
			this.onSeamlessVolumeChangingStarted(param3);
			this.fSeamlessVolumeChangingTween_tw=new Tween(this, "volumeValue", None.easeInOut, this.volumeValue, param1, param2 / 1000, true);
			this.fSeamlessVolumeChangingTween_tw.addEventListener(TweenEvent.MOTION_FINISH, this.onSeamlessVolumeChangingCompleted);
			return;
		} // end function

		public function set soundOn(param1:Boolean):void
		{
			this.fSoundOn_bl=param1;
			this.onVolumeValueUpdated();
			return;
		} // end function

		public function get soundOn():Boolean
		{
			return this.fSoundOn_bl;
		} // end function

		private function checkSetVolumeParams(param1:Number, param2:Number=0):void
		{
			if (param1 < 0 || param1 > 1)
			{
				throw Error("Sound volume value must be from 0 to 1.");
			}
			if (param2 < 0)
			{
				throw Error("Time interval cannot be less than 0.");
			}
			return;
		} // end function

		private function onSeamlessVolumeChangingStarted(param1:Function=null):void
		{
			this.dispatchEvent(new Event(EVENT_SEAMLESS_VOLUME_CHANGING_STARTED));
			if (Boolean(param1))
			{
				this.param1(this);
			}
			return;
		} // end function

		public function set volumeValue(param1:Number):void
		{
			this.fCurrentSoundVolume_num=param1;
			this.onVolumeValueUpdated();
			return;
		} // end function

		private function setNextVolumeValue():void
		{
			var _loc_1:Number=NaN;
			_loc_1=this.fCurrentSoundVolume_num + this.fSeamlessVolumeChangingDelta_num;
			if (_loc_1 < this.fNewVolumeValueForSeamlessVolumeChanging_num && this.fSeamlessVolumeChangingDelta_num < 0 || _loc_1 > this.fNewVolumeValueForSeamlessVolumeChanging_num && this.fSeamlessVolumeChangingDelta_num > 0)
			{
				_loc_1=this.fNewVolumeValueForSeamlessVolumeChanging_num;
			}
			this.fCurrentSoundVolume_num=_loc_1;
			this.onVolumeValueUpdated();
			if (this.fCurrentSoundVolume_num == this.fNewVolumeValueForSeamlessVolumeChanging_num)
			{
			}
			else
			{
				this.fSeamlessVolumeChangingTimeoutId_uint=setTimeout(setNextVolumeValue, this.fVolumeUpdateIntervalInMs_num);
			}
			return;
		} // end function

		public function get soundTransform():SoundTransform
		{
			return this.fSoundTransform_st;
		} // end function

		public function resetAllDefferedVolumeChanges():void
		{
			var _loc_1:int=0;
			_loc_1=0;
			while (_loc_1 < this.fDefferedVolumeChangeControllers_uint_arr.length)
			{

				clearTimeout(this.fDefferedVolumeChangeControllers_uint_arr[_loc_1]);
				_loc_1++;
			}
			this.fDefferedVolumeChangeControllers_uint_arr=new Array();
			return;
		} // end function

		private function stopSeamlessVolumeChanging():void
		{
			if (this.fSeamlessVolumeChangingTween_tw)
			{
				this.fSeamlessVolumeChangingTween_tw.stop();
			}
			clearTimeout(this.fSeamlessVolumeChangingTimeoutId_uint);
			return;
		} // end function

		public function get volumeValue():Number
		{
			return this.fCurrentSoundVolume_num;
		} // end function

	}
}
